package org.jeecg.modules.demo.crm.mapper;

import java.math.BigDecimal;
import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.modules.demo.crm.entity.CInvoiceSplit;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.jeecg.modules.demo.crm.entity.CPaymentRequest;

/**
 * @Description: 发票拆分
 * @Author: jeecg-boot
 * @Date:   2025-06-21
 * @Version: V1.0
 */
public interface CInvoiceSplitMapper extends BaseMapper<CInvoiceSplit> {

    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_split where invoice_id =#{invoiceId} and request_id=#{requestId}")
    BigDecimal findbyInvoiceAndRequestId(@Param("invoiceId") String invoiceId,@Param("requestId") String requestId);

    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_split where invoice_id =#{invoiceId}")
    BigDecimal findbyInvoice(@Param("invoiceId") String invoiceId);

    @Select("select t2.*  from c_invoice_split as t1 left join c_payment_request as t2 on t1.request_id = t2.id   where t1.invoice_id =#{invoiceId}")
    List<CPaymentRequest> findbyInvoiceQRequestId(@Param("invoiceId") String invoiceId);


    @Select("select  IFNULL(SUM(split_amount), 0)  from c_invoice_split where invoice_id =#{invoiceId} and request_id!=#{requestId}")
    BigDecimal findbyInvoiceNot(@Param("invoiceId") String invoiceId,@Param("requestId") String requestId);

    @Delete("delete from c_invoice_split where request_id =#{requestId}")
    void deleteSplit(@Param("requestId") String requestId);
}
